home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Utilities / KPerfMon / Source / Monitor.m < prev    next >
Text File  |  1992-11-17  |  10KB  |  267 lines

  1. /*  Modified by Andrew Loewenstern (andrew@cubetech.com) <drue> */
  2.  /*  7/15/92 for 3.0 compatibility.  Mainly changed headers and */
  3.  /*  such... */
  4.  
  5. /*
  6.   Copyright 1991 Scott Hess.  Permission to use, copy, modify, and
  7.   distribute this software and its documentation for any purpose
  8.   and without fee is hereby granted, provided that this copyright
  9.   notice appear in all copies.  The copyright notice need not appear
  10.   on binary-only distributions - just in source code.
  11.   
  12.   Scott Hess makes no representations about the suitability of this
  13.   software for any purpose.  It is provided "as is" without express
  14.   or implied warranty.
  15. */
  16.  
  17. /* Generated by Interface Builder */
  18.  
  19. #import "table_compat.h"    /* header file with stuff that was */
  20.                 /* nuked in 3.0 -- drue */
  21.  
  22. //#import <mach.h>
  23. //#import <defaults.h>        /* these 2 changed in 3.0 -- drue */
  24.  
  25. #import <mach/mach.h>
  26. #import <defaults/defaults.h>
  27.  
  28.  
  29. #import <string.h>
  30. #import <sys/dk.h>
  31.  
  32.  
  33. // #import <sys/table.h>    /* changed in 3.0 -- drue */
  34. #import <bsd/machine/table.h>
  35.  
  36. #import <objc/Storage.h>
  37. #import <dpsclient/psops.h>
  38. #import <dpsclient/wraps.h>
  39. #import <appkit/nextstd.h>
  40. #import <appkit/Application.h>
  41. #import <appkit/Matrix.h>
  42. #import "Monitor.h"
  43. #import "AppDelegate.h"
  44. #import "wraps.h"
  45.  
  46. const int elementsToDisplay = 55;    // we display the last <x> events
  47. const int elementsToSave = 250;        // but keep around some extras...
  48.  
  49. extern int table(int,int,void *,int,int);    // We need to stick this in because the table call is undocumented.
  50.  
  51. @implementation Monitor
  52.  
  53. - initFrame:(NXRect *)frameRect
  54. {
  55.     /**************************************************************************************************************************
  56.      *    Instantiate the NXImages needed for compositing onto the icon (titles and such).                                    *
  57.      **************************************************************************************************************************/
  58.     cpuTiff = [NXImage findImageNamed:"CPU.tiff"];
  59.     vmTiff = [NXImage findImageNamed:"VM.tiff"];
  60.     memTiff = [NXImage findImageNamed:"MEM.tiff"];
  61.     diskTiff = [NXImage findImageNamed:"DISK.tiff"];
  62.     networkTiff = [NXImage findImageNamed:"NET.tiff"];
  63.  
  64.     /**************************************************************************************************************************
  65.      *    Instantiate the Storage Object needed to keep the statistics                                                        *
  66.      **************************************************************************************************************************/
  67.     stats = [Storage alloc];
  68.     [stats initCount:0 
  69.            elementSize:sizeof(struct perfData) 
  70.            description:PERFDATA_DESC];
  71.     
  72.     [super initFrame:frameRect];   
  73.  
  74.     infoType = -1;
  75.     return self;
  76. }
  77.  
  78. - drawSelf:(NXRect *)rects :(int)rectCount
  79. {
  80. unsigned int total;
  81. struct perfData *old;
  82.     
  83. /*   Note: I should have just translated the coordinates instead of adding 8 to everything, but I was too stupid to think of it at the time. Change it later. */
  84.     switch ( infoType )
  85.         {
  86.         case CPU:
  87.             drawCPU(current.system / 2 + 8,
  88.                     ( current.user + current.system ) / 2 + 8,
  89.                     ( current.user + current.system + current.nice) / 2 + 8);
  90.             break;
  91.         case MEMORY:
  92.             total = current.free + current.active + current.inactive + current.wired;
  93.             drawMem(current.wired * 50 / total + 8,
  94.                    ( current.wired + current.inactive ) * 50 / total + 8,
  95.                    ( current.wired + current.inactive + current.active ) * 50 / total + 8);
  96.             break;
  97.         case DISK:
  98.             drawDisk(MIN(current.disk,50) + 8,
  99.                      MIN(current.pagein + current.pageout,50) + 8,
  100.                      MIN(current.pageout,50) + 8);
  101.             break;
  102.         case NETWORK:
  103.             if ( (old = (struct perfData *)[stats elementAt:[stats count]-2]) == NULL )
  104.                 drawNet(8,8,8,MIN(current.netin,50)+8,MIN(current.netout,50)+8,MIN(current.netcollision,50)+8);
  105.             else
  106.                 drawNet(MIN(old->netin,50)+8,MIN(old->netout,50)+8,MIN(old->netcollision,50)+8,
  107.                         MIN(current.netin,50)+8,MIN(current.netout,50)+8,MIN(current.netcollision,50)+8);
  108.             break;
  109.         }
  110.     return self;
  111. }
  112.  
  113.  
  114. - flip:sender
  115. {
  116. static NXPoint aPoint = {2,0};
  117. struct perfData *data;
  118. struct perfData *old;
  119. int i;
  120. int type;
  121. int count;
  122. int total;
  123.  
  124.     if ( (type = [[sender selectedCell] tag]) == infoType )
  125.         return self;   
  126.     [self lockFocus];
  127.     count = [stats count];
  128.     switch ( type )
  129.         {
  130.         case CPU:
  131.             infoType = CPU;
  132.             [cpuTiff composite:NX_COPY toPoint:&aPoint];
  133.             NXWriteDefault([NXApp appName],"InfoType","0");
  134.             for ( i = MAX(count - elementsToDisplay,0) ; i < count ; i++ )
  135.                 {
  136.                 data = (struct perfData *)[stats elementAt:i];
  137.                 drawCPU(data->system / 2 + 8,
  138.                         ( data->user + data->system ) / 2 + 8,
  139.                         ( data->user + data->system + data->nice) / 2 + 8);
  140.                 }
  141.             break;
  142.         case MEMORY:
  143.             infoType = MEMORY;
  144.             [memTiff composite:NX_COPY toPoint:&aPoint];
  145.             NXWriteDefault([NXApp appName],"InfoType","1");
  146.             for ( i = MAX(count - elementsToDisplay,0) ; i < count ; i++ )
  147.                 {
  148.                 data = (struct perfData *)[stats elementAt:i];
  149.                 total = data->free + data->active + data->inactive + data->wired;                
  150.                 drawMem(data->wired * 50 / total + 8,
  151.                        ( data->wired + data->inactive ) * 50 / total + 8,
  152.                        ( data->wired + data->inactive + data->active ) * 50 / total + 8);
  153.                 }
  154.             break;
  155.         case DISK:
  156.             infoType = DISK;
  157.             [diskTiff composite:NX_COPY toPoint:&aPoint];
  158.             NXWriteDefault([NXApp appName],"InfoType","2");
  159.             for ( i = MAX(count - elementsToDisplay,0) ; i < count ; i++ )
  160.                 {
  161.                 data = (struct perfData *)[stats elementAt:i];
  162.                 drawDisk(MIN(data->disk,50) + 8,
  163.                          MIN(data->pagein + data->pageout,50) + 8,
  164.                          MIN(data->pageout,50) + 8);
  165.                 }
  166.             break;
  167.         case NETWORK:
  168.             infoType = NETWORK;
  169.             [networkTiff composite:NX_COPY toPoint:&aPoint];
  170.             NXWriteDefault([NXApp appName],"InfoType","3");
  171.             for ( i = MAX(count - elementsToDisplay,1) ; i < count ; i++ )
  172.                 {
  173.                 old = (struct perfData *)[stats elementAt:i-1];
  174.                 data = (struct perfData *)[stats elementAt:i];
  175.                 drawNet(MIN(old->netin,50)+8,MIN(old->netout,50)+8,MIN(old->netcollision,50)+8,
  176.                         MIN(data->netin,50)+8,MIN(data->netout,50)+8,MIN(data->netcollision,50)+8);
  177.                 }
  178.             break;
  179.         }
  180.     [self unlockFocus];
  181.     [window flushWindow];
  182.  
  183.     return self;
  184. }
  185.  
  186. - step
  187. {
  188. struct tbl_cpuinfo tc;
  189. long cp_time[CPUSTATES];
  190. long total;
  191. long temp;
  192. vm_statistics_data_t vmstat;
  193. struct tbl_netinfo netstat;
  194. struct tbl_diskinfo diskstat;
  195. struct tbl_ioinfo iostat;
  196.  
  197.     /**************************************************************************************************************************
  198.      *    Get the data                                                                                                        *
  199.      **************************************************************************************************************************/
  200.     table(TBL_CPUINFO,0,&tc,1,sizeof(tc));
  201.     bcopy(tc.ci_cptime,cp_time,sizeof(cp_time));
  202.     vm_statistics(task_self(),&vmstat);
  203.     table(TBL_NETINFO,0,&netstat,1,sizeof(netstat));
  204.     table(TBL_DISKINFO,0,&diskstat,1,sizeof(diskstat));
  205.     table(TBL_IOINFO,0,&iostat,1,sizeof(iostat));
  206.  
  207.     /**************************************************************************************************************************
  208.      *    Process the data into the proper form.                                                                              *
  209.      **************************************************************************************************************************/
  210.     total = cp_time[CP_SYS] + cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_IDLE];
  211.     temp = total;
  212.     
  213.         if((total - oldTotal) > 0)  /* this was done to fix the div0 error.  It */
  214.             total -= oldTotal;        /* works, but barely...  - drue  */
  215.     oldTotal = temp;
  216.     
  217.     current.system = ( cp_time[CP_SYS] - oldTimes[CP_SYS] ) * 100 / total;
  218.     current.user = ( cp_time[CP_USER] - oldTimes[CP_USER] ) * 100 / total;
  219.     current.nice = ( cp_time[CP_NICE] - oldTimes[CP_NICE] ) * 100 / total;
  220.     bcopy(cp_time,oldTimes,sizeof(cp_time));
  221.  
  222.     current.free = vmstat.free_count;
  223.     current.active = vmstat.active_count;
  224.     current.inactive = vmstat.inactive_count;
  225.     current.wired = vmstat.wire_count;
  226.  
  227.     current.netin = ( netstat.ni_ipackets - oldin ) / pollInterval;
  228.     current.netout = ( netstat.ni_opackets - oldout ) / pollInterval;
  229.     current.netcollision = ( netstat.ni_collisions - oldcollision ) / pollInterval;
  230.     oldin = netstat.ni_ipackets;
  231.     oldout = netstat.ni_opackets;
  232.     oldcollision = netstat.ni_collisions;
  233.  
  234.     current.disk = ( diskstat.di_xfer - olddisk) / pollInterval;
  235.     current.pagein = ( vmstat.pageins - oldpagein ) / pollInterval;
  236.     current.pageout = ( vmstat.pageouts - oldpageout ) / pollInterval;
  237.     olddisk = diskstat.di_xfer;
  238.     oldpagein = vmstat.pageins;
  239.     oldpageout = vmstat.pageouts;
  240.  
  241.     [self display];
  242.  
  243.     if ( [stats count] > elementsToSave ) {
  244.     [stats removeElementAt:0];
  245.     }
  246.     
  247.     [stats addElement:¤t];
  248.     return self;
  249. }   
  250.  
  251. @end
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.